// pages/Books/Books.js
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
  
    currentTab: 0,
    navScrollLeft: 0,
    bookTitle:['词牌', '主题', '朝代', '名山','节日','花卉'],
    currentIndex:'',
   
    img:['https://s3.bmp.ovh/imgs/2022/04/18/cb06427a47355f51.png', 'https://s3.bmp.ovh/imgs/2022/04/18/1bd509a21e1b23ce.png', 'https://s3.bmp.ovh/imgs/2022/04/18/fd48de5e548dc578.png', 'https://s3.bmp.ovh/imgs/2022/04/18/ca6ac55c6d80ba78.png','https://s3.bmp.ovh/imgs/2022/04/18/4e537439d2bbaae9.png','https://s3.bmp.ovh/imgs/2022/04/18/b223273320e93458.png'] ,

    // 魏晋 https://s3.bmp.ovh/imgs/2022/05/13/911aee6a643beda8.png

    //唐 https://s3.bmp.ovh/imgs/2022/05/16/2cc57d306219b6e9.png
    //宋 https://s2.loli.net/2022/05/22/eGZSQ61s75iFmDu.png
    //元 https://s2.loli.net/2022/05/22/8Rh1ZXoBWxz39GN.png
    //明https://s2.loli.net/2022/05/22/AGjK8qg7w6pJzBs.png
    //清  https://s2.loli.net/2022/05/22/k81ftQc3avluHRF.png
    //南北朝  https://s2.loli.net/2022/05/22/q2Ad9rSmCM3TaXZ.png
    img1:[],

    // img1:['https://s3.bmp.ovh/imgs/2022/05/16/2cc57d306219b6e9.png','https://s2.loli.net/2022/05/22/eGZSQ61s75iFmDu.png', 'https://s2.loli.net/2022/05/22/8Rh1ZXoBWxz39GN.png',  'https://s2.loli.net/2022/05/22/AGjK8qg7w6pJzBs.png','https://s2.loli.net/2022/05/22/k81ftQc3avluHRF.png','https://s2.loli.net/2022/05/22/q2Ad9rSmCM3TaXZ.png','https://s3.bmp.ovh/imgs/2022/05/13/911aee6a643beda8.png'] ,
    cipai:[], //词牌名
    cipaiNum:[],
    cipaiJump:[],
 
    theme:[], //主题
    themeNum:[], 
    myTheme:[{
      "主题名":"哲理诗",
      "数目":12
      },
      {
        "主题名":"山水田园",
        "数目":89
      }], //[{},{}]

    dynasty:[],     // 诗人
    dynastyNum:[], //不同朝代诗的数量
    dynastyJump:[], // 某朝代诗详细页面

    mountain:[], //名山
    mountainNum:[],

    festival:[], //节日
    festivalNum:[],

    flower:[],     // 花卉
    flowerNum:[], //
    dynastyJump:[], //  

    


  },

  onLoad: function () {
    this.getCipai();//获取词牌名分类
    this.getTheme();//获取词牌名分类
    this.getDynastys(); //获取朝代分类
    this.getMountain();//获取名山分类
    this.getFestival(); //获取节日分类
    this.getFlower();//获取花卉分类
  
  },
    /* 获取具体主题 */ 
    jumpTheme:function(e){
 
      // console.log(e);
      let that = this; 
      wx.request({   
        url: `${app.globalData.url}/classify/detail`,
        header: { 
            'Content-Type': 'application/json'
        },
        data: {
          clevel2:e.currentTarget.id
   
        },
      
        success: function(res) {
   
          that.setData({
            themeJump:res.data
          })
          var data = JSON.stringify(res.data)
          console.log(data)
          wx.navigateTo({  
            url:"/pages/Book-theme/Book-theme?data="+encodeURIComponent(data)+ "&dataId="+e.currentTarget.id
          })
        }
      })
  
      
    },
    /* 获取主题 */
    getTheme: function(){
      let that = this;
      let arrFes = [];
      
      wx.request({   
        url: `${app.globalData.url}/classify/`,
        header: { 
            'Content-Type': 'application/json'
        },
        data: {//这里写你要请求的参数
          // clevel2:"唐"
          // Uid:that.data.Uid
        },
      
        success: function(res) {
          // console.log(res.data)
          for (var i=0;i<res.data.length;i++) {
  
            if(res.data[i].clevel1 === "主题") {
         
              arrFes.push(res.data[i].clevel2)
  
            }
          }
          
          for(var i=0;i<arrFes.length;i++) {
            console.log(arrFes[i]); 
            that.getClassifyNumTheme(arrFes[i]) 
            
          }
          // that.setData({
          //   theme:arrFes
          // })
     
        }
      })
        
    },
    /* 获取主题clevel2数量 */
    getClassifyNumTheme: function(cle2){
      let that = this;
      let titleNum = [];
      let arrTheme = [];
      wx.request({   
        url: `${app.globalData.url}/classify/detail`,
        header: { 
            'Content-Type': 'application/json'
        },
        data: {//这里写你要请求的参数
          clevel2:cle2
          // Uid:that.data.Uid
        }, 
       
        success: function(res) {
          console.log(cle2,res.data)
          var lengthNum = that.changeNumMoneyToChinese(res.data.length)
          
          arrTheme.push(...that.data.theme,cle2)
          titleNum.push(...that.data.themeNum,lengthNum)
          console.log(titleNum)
          
          // console.log('two',titleNum)
          that.setData({
            theme:arrTheme,
            themeNum: titleNum
          })
   
        }
      })
        
    },


  /* 获取具体词牌名 */ 
  jumpCipai:function(e){
 
    // console.log(e);
    let that = this; 
    wx.request({   
      url: `${app.globalData.url}/classify/detail`,
      header: { 
          'Content-Type': 'application/json'
      },
      data: {
        clevel2:e.currentTarget.id
 
      },
    
      success: function(res) {
 
        that.setData({
          cipaiJump:res.data
        })
        var data = JSON.stringify(res.data)
        // console.log(data)
        wx.navigateTo({  
          url:"/pages/Book-cipai/Book-cipai?data="+encodeURIComponent(data)+ "&dataId="+e.currentTarget.id
        })
      }
    })

    
  },
  /* 获取词牌名 */
  getCipai: function(){
    let that = this;
    let arrFes = [];
    
    wx.request({   
      url: `${app.globalData.url}/classify/`,
      header: { 
          'Content-Type': 'application/json'
      },
      data: {//这里写你要请求的参数
        // clevel2:"唐"
        // Uid:that.data.Uid
      },
    
      success: function(res) {
       
        for (var i=0;i<res.data.length;i++) {

          if(res.data[i].clevel1 === "词牌名") {
            // console.log(res.data[i])
            arrFes.push(res.data[i].clevel2)

          }
        }

        for(var i=0;i<arrFes.length;i++) {
          that.getClassifyNumCipai(arrFes[i]) 
        }
       
   
      }
    })
      
  },
  /* 获取词牌名clevel2数量 */
  getClassifyNumCipai: function(cle2){
    let that = this;
    let titleNum = [];
    let arrCipai = [];
    wx.request({   
      url: `${app.globalData.url}/classify/detail`,
      header: { 
          'Content-Type': 'application/json'
      },
      data: {//这里写你要请求的参数
        clevel2:cle2
        // Uid:that.data.Uid
      },
    
      success: function(res) {
        // console.log(res.data)
        var lengthNum = that.changeNumMoneyToChinese(res.data.length)

        arrCipai.push(...that.data.cipai,cle2)
        titleNum .push(...that.data.cipaiNum,lengthNum)
  
        
        // console.log('two',titleNum)
        that.setData({
          cipai:arrCipai,
          cipaiNum: titleNum
        })
 
      }
    })
      
  },

  /* 获取具体朝代诗 */ 
  jumpDynasty:function(e){
    console.log(e,e.currentTarget.id);
    let that = this;
    wx.request({   
      url: `${app.globalData.url}/classify/detail`,
      header: { 
          'Content-Type': 'application/json'
      },
      data: {//这里写你要请求的参数
        clevel2: e.currentTarget.id
        //  
      },
    
      success: function(res) {
        // console.log(res.data)
        that.setData({
          dynastyJump:res.data
        })
        var data = JSON.stringify(res.data)
        wx.navigateTo({ /* ?Wid= "+petsnum */
          url:"/pages/Book-poem/Book-poem?data="+encodeURIComponent(data)+ "&dataId="+e.currentTarget.id
        })
      }
    })

    
  },
  /* 获取朝代clevel2 数量 */
  getClassifyNumDynasty: function(cle2){
    let that = this;
    let titleNum = [];
    let arrDynasty = [];
    wx.request({   
      url: `${app.globalData.url}/classify/detail`,
      header: { 
          'Content-Type': 'application/json'
      },
      data: {//这里写你要请求的参数
        clevel2:cle2
        // Uid:that.data.Uid
      },
    
      success: function(res) {
        // console.log(res.data)
        var lengthNum = that.changeNumMoneyToChinese(res.data.length);

        arrDynasty.push(...that.data.dynasty,cle2)
        titleNum .push(...that.data.dynastyNum,lengthNum)
  
        
        // console.log('two',titleNum)
        that.setData({
          dynasty:arrDynasty,
          dynastyNum: titleNum
        })
 
      }
    })
      
  },
  /* 获取朝代 */
  getDynastys: function(){
    let that = this;
    let arrFes = [];
    let dyImg = [];
    wx.request({   
      url: `${app.globalData.url}/classify/`,
      header: { 
          'Content-Type': 'application/json'
      },
      data: {//这里写你要请求的参数
        // clevel2:"唐"
        // Uid:that.data.Uid
      },
    
      success: function(res) {
   
        for (var i=0;i<res.data.length;i++) {
 
          if(res.data[i].clevel1 === "朝代") {
            console.log(res.data[i])
            dyImg.push(res.data[i].cimg)
            arrFes.push(res.data[i].clevel2)

          }
        }
        that.setData({
          img1:dyImg
        })
        // console.log(dyImg)
        for(var i=0;i<arrFes.length;i++) {
       
          that.getClassifyNumDynasty(arrFes[i]) 
 
        }
         
   
      }
    })
      
  },
 
  /* 获取具体名山 */ 
  jumpMountain:function(e){
 
    console.log(e);
    let that = this; 
    wx.request({   
      url: `${app.globalData.url}/classify/detail`,
      header: { 
          'Content-Type': 'application/json'
      },
      data: {
        clevel2:e.currentTarget.id
 
      },
    
      success: function(res) {
 
        that.setData({
          // themeJump:res.data
        })
        var data = JSON.stringify(res.data)
        // console.log(data)
        wx.navigateTo({  
          url:"/pages/Book-mountain/Book-mountain?data="+encodeURIComponent(data)+ "&dataId="+e.currentTarget.id
        })
      }
    })

    
  },
  /* 获取名山 */
  getMountain: function(){
    let that = this;
    let arrFes = [];
    
    wx.request({   
      url: `${app.globalData.url}/classify/`,
      header: { 
          'Content-Type': 'application/json'
      },
      data: {//这里写你要请求的参数
        // clevel2:"唐"
        // Uid:that.data.Uid
      },
    
      success: function(res) {
       
        for (var i=0;i<res.data.length;i++) {

          if(res.data[i].clevel1 === "名山") {
            // console.log(res.data[i])
            arrFes.push(res.data[i].clevel2)

          }
        }
        console.log(arrFes)
        for(var i=0;i<arrFes.length;i++) {
          that.getClassifyNumMountain(arrFes[i]) 
        }
         
      }
    })
      
  },
  /* 获取名山clevel2数量 */
  getClassifyNumMountain: function(cle2){
    let that = this;
    let titleNum = [];
    let arrMountain = [];
    wx.request({   
      url: `${app.globalData.url}/classify/detail`,
      header: { 
          'Content-Type': 'application/json'
      },
      data: {//这里写你要请求的参数
        clevel2:cle2
        // Uid:that.data.Uid
      },
    
      success: function(res) {
        // console.log(res.data)
        var lengthNum = that.changeNumMoneyToChinese(res.data.length)

        arrMountain.push(...that.data.mountain,cle2)

        titleNum.push(...that.data.mountainNum,lengthNum)
  
        
        // console.log('two',titleNum)
        that.setData({
          mountain:arrMountain,
          mountainNum: titleNum
        })
 
      }
    })
      
  },

  /* 获取具体节日 */ 
  jumpFestival:function(e){
 
      console.log(e);
      let that = this; 
      wx.request({   
        url: `${app.globalData.url}/classify/detail`,
        header: { 
            'Content-Type': 'application/json'
        },
        data: {
          clevel2:e.currentTarget.id
   
        },
      
        success: function(res) {
   
          that.setData({
            // themeJump:res.data
          })
          var data = JSON.stringify(res.data)
          // console.log(data)
          wx.navigateTo({  
            url:"/pages/Book-festival/Book-festival?data="+encodeURIComponent(data)+ "&dataId="+e.currentTarget.id
          })
        }
      })
  
      
    },
  /* 获取节日 */
  getFestival: function(){
      let that = this;
      let arrFes = [];
      
      wx.request({   
        url: `${app.globalData.url}/classify/`,
        header: { 
            'Content-Type': 'application/json'
        },
        data: {//这里写你要请求的参数
          // clevel2:"唐"
          // Uid:that.data.Uid
        },
      
        success: function(res) {
         
          for (var i=0;i<res.data.length;i++) {
            // console.log(res.data[i])

            if(res.data[i].clevel1 === "节日") {
              console.log(res.data[i])
              arrFes.push(res.data[i].clevel2)
  
            }
          }
          console.log(arrFes)
          for(var i=0;i<arrFes.length;i++) {
            that.getClassifyNumTFestival(arrFes[i]) 
          }
         
     
        }
      })
        
    },
  /* 获取节日clevel2数量 */
  getClassifyNumTFestival: function(cle2){
    let that = this;
    let titleNum = [];
    let arrFestival = [];
    wx.request({   
      url: `${app.globalData.url}/classify/detail`,
      header: { 
          'Content-Type': 'application/json'
      },
      data: {//这里写你要请求的参数
        clevel2:cle2
        // Uid:that.data.Uid
      },
    
      success: function(res) {
        // console.log(res.data)
        var lengthNum = that.changeNumMoneyToChinese(res.data.length)

        arrFestival.push(...that.data.festival,cle2)

        titleNum.push(...that.data.festivalNum,lengthNum)
  
        
        // console.log('two',titleNum)
        that.setData({
          festival:arrFestival,
          festivalNum: titleNum
        })
  
      }
    })
      
  },

  /* 获取具体花卉 */ 
  jumpFlower:function(e){
 
    console.log(e);
    let that = this; 
    wx.request({   
      url: `${app.globalData.url}/classify/detail`,
      header: { 
          'Content-Type': 'application/json'
      },
      data: {
        clevel2:e.currentTarget.id
 
      },
    
      success: function(res) {
 
        that.setData({
          // themeJump:res.data
        })
        var data = JSON.stringify(res.data)
        // console.log(data)
        wx.navigateTo({  
          url:"/pages/Book-flower/Book-flower?data="+encodeURIComponent(data) + "&dataId="+e.currentTarget.id
        })
      }
    })

    
  },
  /* 获取花卉 */
  getFlower: function(){
    let that = this;
    let arrFes = [];
    
    wx.request({   
      url: `${app.globalData.url}/classify/`,
      header: { 
          'Content-Type': 'application/json'
      },
      data: {//这里写你要请求的参数
        // clevel2:"唐"
        // Uid:that.data.Uid
      },
    
      success: function(res) {
        
        for (var i=0;i<res.data.length;i++) {

          if(res.data[i].clevel1 === "花卉") {
            // console.log(res.data[i])
            arrFes.push(res.data[i].clevel2)

          }
        }
 
        for(var i=0;i<arrFes.length;i++) {
          that.getClassifyNumTFlower(arrFes[i]) 
        }
        
    
      }
    })
      
  },
  /* 获取花卉clevel2数量 */
  getClassifyNumTFlower: function(cle2){
  let that = this;
  let titleNum = [];
  let arrFlower = [];
  wx.request({   
    url: `${app.globalData.url}/classify/detail`,
    header: { 
        'Content-Type': 'application/json'
    },
    data: {//这里写你要请求的参数
      clevel2:cle2
      // Uid:that.data.Uid
    },

    success: function(res) {
      // console.log(res.data)
      var lengthNum = that.changeNumMoneyToChinese(res.data.length)

      arrFlower.push(...that.data.flower,cle2)
      titleNum.push(...that.data.flowerNum,lengthNum)

      
      // console.log('two',titleNum)
      that.setData({
        flower:arrFlower,
        flowerNum: titleNum
      })

    }
  })
    
  },

  switchNav(event){

    // 获取当前tab 的id
    var cur = event.currentTarget.dataset.current; 
  
    //每个tab选项宽度占1/5
    var singleNavWidth = this.data.windowWidth / 5;

    //tab选项居中                            
    this.setData({
        navScrollLeft: (cur - 2) * singleNavWidth
    })   
    // 判断id是否和点击的tab id 一致   
    if (this.data.currentTab == cur) {
        return false;
    } else {
      // console.log("====",cur)
        this.setData({
            currentTab: cur
        })
    }
  },

  switchTab(event){
      var cur = event.detail.current;
      var singleNavWidth = this.data.windowWidth / 5;
      this.setData({
          currentTab: cur,
          navScrollLeft: (cur - 2) * singleNavWidth
      });
  },


  /* 将数字转为大写 */
  changeNumMoneyToChinese: function (IntegerNum) {
    var cnNums = new Array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"); //汉字的数字
    var cnIntRadice = new Array("", "拾", "佰", "仟"); //基本单位
    var cnIntUnits = new Array("", "万", "亿", "兆"); //对应整数部分扩展单位
    var ChineseStr = ""; //输出的中文金额字符串
    IntegerNum = IntegerNum.toString();
      var zeroCount = 0;
      var IntLen = IntegerNum.length;
      for (var i = 0; i < IntLen; i++) {
          var n = IntegerNum.substr(i, 1);
          var p = IntLen - i - 1;
          var q = p / 4;
          var m = p % 4;
          if (n == "0") {
            if(IntLen === 1 && n==="0") {
              ChineseStr = cnNums[0]
            }
              zeroCount++;
          }
          else {
              if (zeroCount > 0) {
                  ChineseStr += cnNums[0];
              }
              zeroCount = 0; //归零
              ChineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
          }
          if (m == 0 && zeroCount < 4) {
              ChineseStr += cnIntUnits[q];
          }
      }
        // console.log(ChineseStr)
    return ChineseStr;
  },
  
 
 /* 标签栏滑动 跳转 */
 changeCurrentIndex:function(e) {
  console.log(e);
  this.setData({
    currentIndex: e.currentTarget.id
  })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // console.log(this.data)
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})